using System;
using System.Data;
using System.Data.SqlClient;

namespace FAS.DataBase
{
    public abstract class ASqlDbController : IDbController
    {
        protected SqlConnection m_objSqlConnection = new SqlConnection();
        protected SqlTransaction m_objSqlTransaction;
        protected int m_intTransactionStatus;
        protected string m_strConnectionString;

        public abstract DataSet Select(string strSQL);
        public abstract DataSet Select(string strSQL, SqlParameter[] objParas);
        public abstract int Execute(string strSQL);
        public abstract int Execute(string strSQL, SqlParameter[] objParas);
        public abstract DataSet SpSelect(string strSqlStatement);
        public abstract DataSet SpSelect(string strSqlStatement, SqlParameter[] objParas);
        public abstract void SpExecute(string strSpName);
        public abstract void SpExecute(string strSpName, SqlParameter[] objParas);
        public abstract string GetPoolingString(bool bIsPoolingOn);

        protected SqlCommand m_objSqlCommand = new SqlCommand();

        public ASqlDbController()
        {
            m_strConnectionString = Connections.getDbConString();
            m_intTransactionStatus = 0;
        }

        public ASqlDbController(bool bIsPoolingOn)
        {
            try
            {
                string result = "";
                string temp = Connections.getDbConString();
                string temp02 = temp.Trim();

                temp02 = temp02.Substring(temp02.Length - 2, 1);
                if (temp02 != ";")
                {
                    temp += ';';
                }

                result = temp + this.GetPoolingString(bIsPoolingOn);

                m_strConnectionString = result;
                m_intTransactionStatus = 0;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public ASqlDbController(string strConnectionString)
        {
            m_strConnectionString = strConnectionString;
            m_intTransactionStatus = 0;
        }

        public bool OpenConnection()
        {
            bool bltResult = false;
            m_objSqlConnection.ConnectionString = m_strConnectionString;
            try
            {
                m_objSqlConnection.Open();
                bltResult = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return bltResult;
        }

        protected SqlConnection GetConnection()
        {
            return m_objSqlConnection;
        }

        public bool CloseConnection()
        {
            bool bltResult = false;
            try
            {
                if (m_objSqlConnection != null)
                {
                    m_objSqlConnection.Close();
                }
                bltResult = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return bltResult;
        }

        public bool BeginTransaction()
        {
            bool bltResult = false;

            try
            {
                m_objSqlTransaction = m_objSqlConnection.BeginTransaction();
                m_objSqlCommand.Transaction = m_objSqlTransaction;
                m_intTransactionStatus = 1;
                bltResult = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return bltResult;
        }

        public bool CommitTransaction()
        {
            bool bltResult = false;

            try
            {
                m_objSqlTransaction.Commit();
                m_intTransactionStatus = 0;
                bltResult = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return bltResult;
        }

        public bool RollbackTransaction()
        {
            bool bltResult = false;
            try
            {
                m_objSqlTransaction.Rollback();
                m_intTransactionStatus = 0;
                bltResult = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return bltResult;
        }

        public int GetTranzactionStatus()
        {
            return m_intTransactionStatus;
        }
    }
}
